//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "LmConstWrapperVolatilityModel.h"
using namespace Cephei::QL::Legacy::Libormarketmodels;
#include <gen/QL/Math/Array.h>
#include <gen/QL/Legacy/Libormarketmodels/LmVolatilityModel.h>
#include <gen/QL/Models/Parameter.h>
using namespace Cephei::QL::Math;
using namespace Cephei::QL::Models;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Legacy::Libormarketmodels::CLmConstWrapperVolatilityModel::CLmConstWrapperVolatilityModel (Cephei::QL::Legacy::Libormarketmodels::ILmVolatilityModel^ volaModel) : CLmVolatilityModel(CLmConstWrapperVolatilityModel::typeid)
{
    CLmVolatilityModel^ _CvolaModel;
    try
    {
#ifdef HANDLE
        _phLmConstWrapperVolatilityModel = NULL;
#endif
        _CvolaModel = safe_cast<CLmVolatilityModel^> (volaModel);
        _CvolaModel->Lock();
        boost::shared_ptr<QuantLib::LmVolatilityModel>& _volaModel = static_cast<boost::shared_ptr<QuantLib::LmVolatilityModel>&> (_CvolaModel->GetShared ()); 
        _ppLmConstWrapperVolatilityModel = new boost::shared_ptr<QuantLib::LmConstWrapperVolatilityModel> (new QuantLib::LmConstWrapperVolatilityModel ( _volaModel ));
        SetLmVolatilityModel (boost::dynamic_pointer_cast<QuantLib::LmVolatilityModel> (*_ppLmConstWrapperVolatilityModel));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_CvolaModel != nullptr) _CvolaModel->Unlock();
    }
}
Cephei::QL::Legacy::Libormarketmodels::CLmConstWrapperVolatilityModel::CLmConstWrapperVolatilityModel (boost::shared_ptr<QuantLib::LmConstWrapperVolatilityModel>& childNative, Object^ owner) : CLmVolatilityModel(CLmConstWrapperVolatilityModel::typeid)
{
#ifdef HANDLE
	_phLmConstWrapperVolatilityModel = NULL;
#endif
	_ppLmConstWrapperVolatilityModel = &childNative;
    _ppLmVolatilityModel = new boost::shared_ptr<QuantLib::LmVolatilityModel> (boost::dynamic_pointer_cast<QuantLib::LmVolatilityModel> (*_ppLmConstWrapperVolatilityModel));
}
Cephei::QL::Legacy::Libormarketmodels::CLmConstWrapperVolatilityModel::CLmConstWrapperVolatilityModel (QuantLib::LmConstWrapperVolatilityModel& childNative, Object^ owner) : CLmVolatilityModel(CLmConstWrapperVolatilityModel::typeid)
{
#ifdef HANDLE
	_phLmConstWrapperVolatilityModel = NULL;
#endif
	_ppLmConstWrapperVolatilityModel = new boost::shared_ptr<QuantLib::LmConstWrapperVolatilityModel> (&childNative);
    _ppLmVolatilityModel = new boost::shared_ptr<QuantLib::LmVolatilityModel> (boost::dynamic_pointer_cast<QuantLib::LmVolatilityModel> (*_ppLmConstWrapperVolatilityModel));
    _LmConstWrapperVolatilityModelOwner = owner;
    _LmVolatilityModelOwner = owner;
}

Cephei::QL::Legacy::Libormarketmodels::CLmConstWrapperVolatilityModel::CLmConstWrapperVolatilityModel (CLmConstWrapperVolatilityModel^ copy) : CLmVolatilityModel(CLmConstWrapperVolatilityModel::typeid)
{
#ifdef HANDLE
	_phLmConstWrapperVolatilityModel = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppLmConstWrapperVolatilityModel = new boost::shared_ptr<QuantLib::LmConstWrapperVolatilityModel> (copy->GetShared());
        _ppLmVolatilityModel = new boost::shared_ptr<QuantLib::LmVolatilityModel> (boost::dynamic_pointer_cast<QuantLib::LmVolatilityModel> (*_ppLmConstWrapperVolatilityModel));
    }
}
Cephei::QL::Legacy::Libormarketmodels::CLmConstWrapperVolatilityModel::CLmConstWrapperVolatilityModel (PLATFORM::Type^ t) : CLmVolatilityModel(CLmConstWrapperVolatilityModel::typeid)
{
#ifdef HANDLE
	_phLmConstWrapperVolatilityModel = NULL;
#endif
	if (!t->IsSubclassOf(CLmConstWrapperVolatilityModel::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Legacy::Libormarketmodels::CLmConstWrapperVolatilityModel::CLmConstWrapperVolatilityModel (QuantLib::Handle<QuantLib::LmConstWrapperVolatilityModel>& childNative, Object^ owner)  : CLmVolatilityModel(CLmConstWrapperVolatilityModel::typeid)
{
	_phLmConstWrapperVolatilityModel = &childNative;
	_ppLmConstWrapperVolatilityModel = &static_cast<boost::shared_ptr<QuantLib::LmConstWrapperVolatilityModel>>(childNative.currentLink());
    _ppLmVolatilityModel = new boost::shared_ptr<QuantLib::LmVolatilityModel> (boost::dynamic_pointer_cast<QuantLib::LmVolatilityModel> (*_ppLmConstWrapperVolatilityModel));
    _LmConstWrapperVolatilityModelOwner = owner;
}
Cephei::QL::Legacy::Libormarketmodels::CLmConstWrapperVolatilityModel::CLmConstWrapperVolatilityModel (QuantLib::Handle<QuantLib::LmConstWrapperVolatilityModel> childNative)  : CLmVolatilityModel(CLmConstWrapperVolatilityModel::typeid)
{
	_phLmConstWrapperVolatilityModel = &childNative;
	_ppLmConstWrapperVolatilityModel = &static_cast<boost::shared_ptr<QuantLib::LmConstWrapperVolatilityModel>>(childNative.currentLink());
    _ppLmVolatilityModel = new boost::shared_ptr<QuantLib::LmVolatilityModel> (boost::dynamic_pointer_cast<QuantLib::LmVolatilityModel> (*_ppLmConstWrapperVolatilityModel));
}
#endif
#ifdef STRUCT
Cephei::QL::Legacy::Libormarketmodels::CLmConstWrapperVolatilityModel::CLmConstWrapperVolatilityModel (QuantLib::LmConstWrapperVolatilityModel childNative)  : CLmVolatilityModel(CLmConstWrapperVolatilityModel::typeid)
{
#ifdef HANDLE
	_phLmConstWrapperVolatilityModel = NULL;
#endif
	_ppLmConstWrapperVolatilityModel = new boost::shared_ptr<QuantLib::LmConstWrapperVolatilityModel> (new QuantLib::LmConstWrapperVolatilityModel (childNative));
    _ppLmVolatilityModel = new boost::shared_ptr<QuantLib::LmVolatilityModel> (boost::dynamic_pointer_cast<QuantLib::LmVolatilityModel> (*_ppLmConstWrapperVolatilityModel));
}
#endif

Cephei::QL::Legacy::Libormarketmodels::CLmConstWrapperVolatilityModel::~CLmConstWrapperVolatilityModel ()
{
    if (_ppLmConstWrapperVolatilityModel != NULL)
    {
	    delete _ppLmConstWrapperVolatilityModel;
        _ppLmConstWrapperVolatilityModel = NULL;
    }
}
Cephei::QL::Legacy::Libormarketmodels::CLmConstWrapperVolatilityModel::!CLmConstWrapperVolatilityModel ()
{
    if (_ppLmConstWrapperVolatilityModel != NULL)
    {
	    delete _ppLmConstWrapperVolatilityModel;
    }
}
QuantLib::LmConstWrapperVolatilityModel& Cephei::QL::Legacy::Libormarketmodels::CLmConstWrapperVolatilityModel::GetReference ()
{
    if (_ppLmConstWrapperVolatilityModel == NULL) throw REFNEW NativeNullException ();
	return **_ppLmConstWrapperVolatilityModel;
}
boost::shared_ptr<QuantLib::LmConstWrapperVolatilityModel>& Cephei::QL::Legacy::Libormarketmodels::CLmConstWrapperVolatilityModel::GetShared ()
{
    if (_ppLmConstWrapperVolatilityModel == NULL) throw REFNEW NativeNullException ();
	return *_ppLmConstWrapperVolatilityModel;
}
QuantLib::LmConstWrapperVolatilityModel* Cephei::QL::Legacy::Libormarketmodels::CLmConstWrapperVolatilityModel::GetPointer ()
{
    if (_ppLmConstWrapperVolatilityModel == NULL) throw REFNEW NativeNullException ();
	return &**_ppLmConstWrapperVolatilityModel;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::LmConstWrapperVolatilityModel>& Cephei::QL::Legacy::Libormarketmodels::CLmConstWrapperVolatilityModel::GetHandle ()
{
	if (_phLmConstWrapperVolatilityModel == NULL)
	{
		_phLmConstWrapperVolatilityModel = new Handle<QuantLib::LmConstWrapperVolatilityModel> (*_ppLmConstWrapperVolatilityModel);
	}
	return *_phLmConstWrapperVolatilityModel;
}
#endif
bool Cephei::QL::Legacy::Libormarketmodels::CLmConstWrapperVolatilityModel::HasNative () 
{
	return (_ppLmConstWrapperVolatilityModel != NULL);
}

Double Cephei::QL::Legacy::Libormarketmodels::CLmConstWrapperVolatilityModel::IntegratedVariance (UInt64 i, UInt64 j, Double u, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Math::IArray^>^ x)
{
    CArray^ _Cx;
    try
    {
        QuantLib::Size _i = (QuantLib::Size)ValueHelper::Convert (i); //a
        QuantLib::Size _j = (QuantLib::Size)ValueHelper::Convert (j); //a
        QuantLib::Time _u = (QuantLib::Time)ValueHelper::Convert (u); //a
        if (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Math::IArray^>::IsSome::get (x))
        {
            _Cx = safe_cast<CArray^> (x->Value);
            _Cx->Lock();
        }
        QuantLib::Array& _x = 
            (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Math::IArray^>::IsSome::get (x) ? static_cast<QuantLib::Array&> (_Cx->GetReference ()) : QuantLib::Null<QuantLib::Array>()); //6
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppLmConstWrapperVolatilityModel)->integratedVariance ( _i,  _j,  _u,  _x );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cx != nullptr) _Cx->Unlock();
    }
}
Double Cephei::QL::Legacy::Libormarketmodels::CLmConstWrapperVolatilityModel::Volatility (UInt64 i, Double t, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Math::IArray^>^ x)
{
    CArray^ _Cx;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::Size _i = (QuantLib::Size)ValueHelper::Convert (i); //a
        QuantLib::Time _t = (QuantLib::Time)ValueHelper::Convert (t); //a
        if (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Math::IArray^>::IsSome::get (x))
        {
            _Cx = safe_cast<CArray^> (x->Value);
            _Cx->Lock();
        }
        QuantLib::Array& _x = 
            (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Math::IArray^>::IsSome::get (x) ? static_cast<QuantLib::Array&> (_Cx->GetReference ()) : QuantLib::Null<QuantLib::Array>()); //6
    	QuantLib::Volatility _rv = (QuantLib::Volatility)(*_ppLmConstWrapperVolatilityModel)->volatility ( _i,  _t,  _x );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cx != nullptr) _Cx->Unlock();
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Legacy::Libormarketmodels::ILmConstWrapperVolatilityModel^ Cephei::QL::Legacy::Libormarketmodels::CLmConstWrapperVolatilityModel_Factory::Create (Cephei::QL::Legacy::Libormarketmodels::ILmVolatilityModel^ volaModel)
{
    return REFNEW CLmConstWrapperVolatilityModel ( volaModel);
}
